import Vue from 'vue'
import axios from 'axios'
import { Notification } from 'element-ui'
import vuex from '@/vuex'
const serverurl = 'http://localhost:8443/'
// 错误处理
const error = function(e) {
  // 弹窗，在右边
  Notification.error({
    title: '通信错误',
    message: e
  })
  return { error: e }
}
const getConfig = function() {
  let config = {
    headers: { Authorization: vuex.state.sessionId || undefined }
  }
  return config
}
const install = function() {
  Vue.prototype.post = async function(api, request) {
    // post
    let response = await axios
      .post(serverurl + api, request, getConfig())
      .then(res => {
        if (res.status === 200) return res.data
      })
      .catch(e => {
        return error(e)
      })
    return response
  }
  Vue.prototype.get = async function(param) {
    // get
  }
}
export default install
